
import os
import time
start = os.listdir("/lianxi/lhj")
while True:
    if start != os.listdir("/lianxi/lhj"):
        end = os.listdir("/lianxi/lhj")
        if len(start)<len(end):
            for i in end:
                if i in start:
                    continue
                else:
                    with open("/lianxi/text.txt", "a") as fp:
                        os.system("stat lhj|grep '最近更改'|awk -F'：' '{print $2}'|awk -F'.' '{print $1}' >>/lianxi/text.txt")
                        fp.write(f"/lianxi/lhj文件下新增了文件{i}\n")
                    start.append(i)

        if len(start)>len(end):
            for i in start:
                if i not in end:
                    with open("/lianxi/text.txt", "w+") as fp:
                        os.system("stat lhj|grep '最近更改'|awk -F'：' '{print $2}'|awk -F'.' '{print $1}'  >>/lianxi/text.txt")
                        # fp.write(str2 )
                        fp.write(f"/lianxi/lhj文件下删除了文件{i}\n")
    else:
        print("/lianxi/lhj没有改变")
    time.sleep(30)